﻿/*
 *       This file is part of VFC.
 *
 *        VFC is free software: you can redistribute it and/or modify
 *        it under the terms of the GNU General Public License as published by
 *        the Free Software Foundation, either version 3 of the License, or
 *        (at your option) any later version.
 *
 *        VFC is distributed in the hope that it will be useful,
 *        but WITHOUT ANY WARRANTY; without even the implied warranty of
 *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *        GNU General Public License for more details.
 *
 *        You should have received a copy of the GNU General Public License
 *        along with VFC.  If not, see <http://www.gnu.org/licenses/>.
 */


using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using VFC.Emu;

namespace VFC.Nes.Mapper
{
    class Nrom
    {
        RomReader _rom;
        UInt16 ctr;

        public Nrom(RomReader rom)
        {
            _rom = rom;
        }

        public void LoadRomToMemory()
        {
            int iCtr=0;

            try
            {
                if (_rom.Count16KbRomBank() == 1)
                {
                    Console.WriteLine("Loading Rom bank 0 to address $8000 - $bfff.");
                    iCtr = 0;
                    ctr = 0x8000;
                    while (ctr <= 0xbfff)
                    {
                        Cpu.Memory.Write(ctr, _rom.RomBank[0,iCtr]);
                        ctr++;
                        iCtr++;
                    }

                    Console.WriteLine("Loading Rom bank 0 to address $c000 - $ffff.");
                    iCtr = 0;
                    ctr = 0xc000;
                    while (ctr <= 0xfffe)
                    {
                        Cpu.Memory.Write(ctr, _rom.RomBank[0, iCtr]);
                        ctr++;
                        iCtr++;
                        //Avoid reaching beyond the boundery of array.
                        //The last part is manually written...
                        if (ctr == 0xffff) { Cpu.Memory.Write(0xffff, _rom.RomBank[0, iCtr]); }
                    }
                }
                else if (_rom.Count16KbRomBank() == 2)
                {
                    Console.WriteLine("Loading Rom bank 0 to address $8000 - $bfff.");
                    iCtr = 0;
                    ctr = 0x8000;
                    while (ctr <= 0xbfff)
                    {
                        Cpu.Memory.Write(ctr, _rom.RomBank[0, iCtr]);
                        ctr++;
                        iCtr++;
                    }

                    Console.WriteLine("Loading Rom bank 1 to address $c000 - $ffff.");
                    iCtr = 0;
                    ctr = 0xc000;
                    while (ctr <= 0xfffe)
                    {
                        Cpu.Memory.Write(ctr, _rom.RomBank[1, iCtr]);
                        ctr++;
                        iCtr++;
                        //Avoid reaching beyond the boundery of array.
                        //The last part is manually written...
                        if (ctr == 0xffff) { Cpu.Memory.Write(0xffff, _rom.RomBank[0, iCtr]); }
                    }
                }
            }
            catch (Exception err)
            {
                Console.WriteLine("ERROR: " + err.Message + " - " + iCtr.ToString());
            }
        }

        public void LoadVRomToPpuMemory()
        {
            int iCtr;
            try
            {
                Console.WriteLine("Loading VRom bank 0 to Ppu address $0000 - $0fff.");
                iCtr = 0;
                ctr = 0x0000;
                while(ctr < 0x0fff)
                {
                    Ppu.Memory.Write(ctr, _rom.VRomBank[0,iCtr]);
                    iCtr++;
                    ctr++;
                }
            }
            catch (Exception err)
            {
                Console.WriteLine("ERROR: " + err.Message);
            }
        }
    }
}
